package com.pithypan.common.utils;

import com.pithypan.common.ResponseCodeEnum;

import java.util.HashMap;
import java.util.Map;

public class R {

    private boolean success;    //请求是否成功
    private Integer code;       //状态码
    private String message;     //消息
    private Map<String, Object> data = new HashMap<String, Object>();  //响应数据

    private R() {}

    //成功静态方法
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResponseCodeEnum.CODE_200.getCode());
        r.setMessage(ResponseCodeEnum.CODE_200.getMsg());
        return r;
    }

    //失败静态方法 -- 默认为服务器错误
    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ResponseCodeEnum.CODE_500.getCode());
        r.setMessage(ResponseCodeEnum.CODE_500.getMsg());
        return r;
    }


    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }



    //get、set属性
    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
}
